$('.number').bind('input propertychange', function () {
    if (this.value < 1) {
        this.value = 1;
    }
});


function getShopInfo(id) {
    $.ajax({
        url: "/Goods/GetGoods?id=" + id,
        async: false,
        success: function (data) {
            var context = "";
            var product = "";


            product += '<div class="img-div">' +
                '<img src="' + data.goods.img + '" onclick="alertModelImg(this)"/>' +
                '</div>';


            if (data.imgList.length > 0 || data.imgList != null) {
                for (var i = 0; i < (data.imgList.length > 3 ? 3 : data.imgList.length); i++) {
                    product += '<div class="img-div">' +
                        '<img src="' + data.imgList[i].url + '" onclick="alertModelImg(this)"/>' +
                        '</div>';
                }
            }

            context = '<div class="row">' +

                '   <div class="col-md-5">' +

                '<div class="tab-content product-details-large" id="myTabContent">' +
                '<div class="tab-pane fade show active" id="single-slide1" role="tabpanel" aria-labelledby="single-slide-tab-1">' +

                '<div class="single-product-img img-full">' +
                '<img id="product-img" src="' + data.goods.img + '" alt="">' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="single-product-menu" id="single-product-menu">' +
                product +
                '</div>' +

                '</div>' +

                '<div class="col-md-7">' +
                '<div class="modal-product-info">' +
                '<h1>' + data.goods.name + '</h1>' +
                '<div class="modal-product-price">' +
                '<span class="new-price">' + data.goods.price + '元</span>' +
                '</div>' +
                '<a href="single-product.html" class="see-all">商品信息</a>' +
                '<div class="add-to-cart quantity">' +
                '<div class="add-quantity">' +
                '<div class="modal-quantity">' +
                '<input type="number" id="number" value="1">' +
                '</div>' +
                '<div class="add-to-link">' +
                '<button class="form-button" data-text="加入购物车" onclick="addCart_Modal(' + data.goods.id + ')">加入购物车</button>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="cart-description">' +
                '<p>' + data.goods.information + '</p>' +
                '</div>' +
                '<div class="social-share">' +
                '<h3>分享商品</h3>' +
                '<ul class="socil-icon2">' +
                '<li><a href=""><i class="fa fa-facebook"></i></a></li>' +
                '<li><a href=""><i class="fa fa-twitter"></i></a></li>' +
                '<li><a href=""><i class="fa fa-pinterest"></i></a></li>' +
                '<li><a href=""><i class="fa fa-google-plus"></i></a></li>' +
                '<li><a href=""><i class="fa fa-linkedin"></i></a></li>' +
                '</ul>' +
                '</div>' +
                '</div>' +
                '</div>' +

                '</div>';

            $(".modal-body").html(context);

            // $("#open-modal").show();

        },
        error: function (err) {
            console.log(err);
        }
    })

}

function alertModelImg(img) {
    $("#product-img").attr("src", img.src);
}


function addCartOne(goodsId) {
    if (goodsId == null || goodsId == "") {
        alert("加入失败请稍后重试");
        return false;
    }

    var user = sessionStorage.getItem("user");
    if (user == null) {
        alert("请登陆后重试！！");
        return false;
    }

    $.ajax({
        url: "/Goods/addCar",
        data: {
            "goodsId": goodsId,
            "userId": $("#userId").val(),
            "number": 1
        },
        type: "POST",
        success: function (response) {
            if (response == "ok") {
                alert("添加成功！！");
            } else {
                alert("添加失败！！！");
            }
        },
        error: function (err) {
            alert("添加失败！！！");
            console.log(err);
        }
    })
}


function addCart(goodsId, num) {
    if (goodsId == null || goodsId == "") {
        alert("加入失败请稍后重试");
        return false;
    }

    if (num == null || num < 1) {
        num = 1;
    }

    var user = sessionStorage.getItem("user");
    if (user == null) {
        alert("请登陆后重试！！");
        return false;
    }


    $.ajax({
        url: "/Goods/addCar",
        data: {
            "goodsId": goodsId,
            "userId": $("#userId").val(),
            "number": num
        },
        type: "POST",
        success: function (response) {
            if (response == "ok") {
                alert("添加成功！！");
            } else {
                alert("添加失败！！！");
            }
        },
        error: function (err) {
            alert("添加失败！！！");
            console.log(err);
        }
    })

}


function addCart_Main(goodsId) {
    addCart(goodsId, $("#number").val());
}

function addCart_Modal(goodsId) {
    addCart(goodsId, $("#number").val());
}

